package com.yiren.manager.ctrl.pension;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.Favorites;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrBaseServiceCost;
import com.yiren.entity.YrBaseServiceCostHistory;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.pension.YrBaseServiceCostForm;
import com.yiren.manager.service.iface.pension.YrBaseServiceCostService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("baseServiceCost")
@Controller
public class YrBaseServiceCostController extends GenericController<YrBaseServiceCostForm, YrBaseServiceCost>{
	
	@Autowired
	private YrBaseServiceCostService yrBaseServiceCostService;

	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrBaseServiceCostForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrBaseServiceCostForm> result = new HTTPResult<YrBaseServiceCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrBaseServiceCostForm form = tool.mapper("baseServiceCost", YrBaseServiceCostForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getBaseServiceCostId()||form.getBaseServiceCostId().isEmpty()){
			form.setBaseServiceCostId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		
		//实体
		YrBaseServiceCost entity = ObjectUtils.objectToObject(form, YrBaseServiceCost.class);
		//修改历史
		YrBaseServiceCostHistory baseServiceCostHistory = ObjectUtils.objectToObject(entity,YrBaseServiceCostHistory.class);
		
		Favorites favorites = new Favorites();
		favorites.putFavorite(YrBaseServiceCostHistory.class, baseServiceCostHistory);
		favorites.putFavorite(YrBaseServiceCost.class, entity);
		
		yrBaseServiceCostService.addByFavorites(favorites);
		
		return result;
	}
	
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrBaseServiceCostForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrBaseServiceCostForm> result = new HTTPResult<YrBaseServiceCostForm>();
		List<YrBaseServiceCost> entityList = yrBaseServiceCostService.findAll();
		List<YrBaseServiceCostForm> formList = new ArrayList<YrBaseServiceCostForm>();
		for(YrBaseServiceCost baseServiceCost : entityList){
			YrBaseServiceCostForm form = ObjectUtils.objectToObject(baseServiceCost, YrBaseServiceCostForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**删除一条记录*/
	@RequestMapping("delete")
	@ResponseBody
	public HTTPResult<YrBaseServiceCostForm> delete(String id) throws MyException{
		HTTPResult<YrBaseServiceCostForm> result = new HTTPResult<YrBaseServiceCostForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		yrBaseServiceCostService.delete(id);
		return result;
	}
	
	/**获取一条记录*/
	@RequestMapping("findOne")
	@ResponseBody
	public HTTPResult<YrBaseServiceCostForm> findOne(String id) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrBaseServiceCostForm> result = new HTTPResult<YrBaseServiceCostForm>();
		YrBaseServiceCost entity = yrBaseServiceCostService.findOne(id);
		List<YrBaseServiceCostForm> formList = new ArrayList<YrBaseServiceCostForm>();
		
		YrBaseServiceCostForm form =  ObjectUtils.objectToObject(entity, YrBaseServiceCostForm.class);

		formList.add(form);
		
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrBaseServiceCostForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrBaseServiceCostForm> result = new HTTPResult<YrBaseServiceCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		for(String id : ids){
			yrBaseServiceCostService.delete(id);
		}
		return result;
	}
	
	@Override
	public YrBaseServiceCostForm toForm(YrBaseServiceCost e) throws Exception {
		YrBaseServiceCostForm form = ObjectUtils.objectToObject(e, YrBaseServiceCostForm.class);
		return form;
	
	}

	@Override
	public YrBaseServiceCost toEntity(YrBaseServiceCostForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
